

if((typeof VideoDecoder) == "undefined"){
  console.log("VideoDecoder unsupport");
} else {
  console.log(typeof VideoDecoder);
}


var WIDTH = 640;
var HEIGHT = 360;



var canvas = document.getElementById('wasmCanvas');
//console.log("get Canvas", canvas);
canvas.width = WIDTH;
canvas.height = HEIGHT;

var ctx = canvas.getContext('2d');
//
config = { 
  codec: 'avc1.424014', 
  width: WIDTH, 
  height: HEIGHT 
}



console.log("hahahaha")
console.log(VideoDecoder.isConfigSupported(config))
const  decoder = new VideoDecoder({
  output(frame) {
    console.log("get a frame", frame);
    ctx.drawImage(frame, 0, 0, frame.displayWidth, frame.displayHeight);
    frame.close();
  },
  error(e) {
    console.log("==> decode", e);
  }
});

decoder.configure(config);

// decoder.configure({ 
//   codec: 'avc1.4D0014', 
//   width: 640, 
//   height: 360 
// });

async function render_video(url) {
  try {
    let response = await fetch(url);
    await response.arrayBuffer().then(function(buffer) {
      // do something with buffer
        console.log(buffer);

        var evc = new EncodedVideoChunk({
          type: "key" ,
           timestamp: 0 ,
          // duration: 1e6 * sample.duration / sample.timescale,
          data: buffer
        }) 
        console.log("==> before decode data");
        decoder.decode(evc);
        console.log("==> after decode data");
        
    });
    await decoder.flush();

    
  } catch (err) {
    console.log(err);
  }
}
render_video("assets/bunny.h264/bunny.h264_0011.h264");